package common.utils;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import java.util.Map;

public class RestTemplateUtil {

    private static final RestTemplate restTemplate = new RestTemplate();

    /**
     * 发送 GET 请求
     *
     * @param url     请求地址
     * @param headers 请求头
     * @param querys  查询参数
     * @return 响应内容
     */
    public static String get(String url, Map<String, String> headers, Map<String, String> querys) {
        // 构建请求头
        HttpHeaders httpHeaders = new HttpHeaders();
        if (headers != null) {
            headers.forEach(httpHeaders::add);
        }

        // 构建 URI，添加查询参数
        UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromHttpUrl(url);
        if (querys != null) {
            querys.forEach(uriBuilder::queryParam);
        }

        // 发送请求
        ResponseEntity<String> response = restTemplate.exchange(
                uriBuilder.build().toUri(),
                HttpMethod.GET,
                new HttpEntity<>(httpHeaders),
                String.class
        );

        // 返回响应内容
        return response.getBody();
    }

    /**
     * 发送 POST 请求
     *
     * @param url     请求地址
     * @param headers 请求头
     * @param body    请求体
     * @return 响应内容
     */
    public static String post(String url, Map<String, String> headers, Object body) {
        // 构建请求头
        HttpHeaders httpHeaders = new HttpHeaders();
        if (headers != null) {
            headers.forEach(httpHeaders::add);
        }
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);

        // 发送请求
        ResponseEntity<String> response = restTemplate.exchange(
                url,
                HttpMethod.POST,
                new HttpEntity<>(body, httpHeaders),
                String.class
        );

        // 返回响应内容
        return response.getBody();
    }

    /**
     * 发送 PUT 请求
     *
     * @param url     请求地址
     * @param headers 请求头
     * @param body    请求体
     * @return 响应内容
     */
    public static String put(String url, Map<String, String> headers, Object body) {
        // 构建请求头
        HttpHeaders httpHeaders = new HttpHeaders();
        if (headers != null) {
            headers.forEach(httpHeaders::add);
        }
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);

        // 发送请求
        ResponseEntity<String> response = restTemplate.exchange(
                url,
                HttpMethod.PUT,
                new HttpEntity<>(body, httpHeaders),
                String.class
        );

        // 返回响应内容
        return response.getBody();
    }

    /**
     * 发送 DELETE 请求
     *
     * @param url     请求地址
     * @param headers 请求头
     * @return 响应内容
     */
    public static String delete(String url, Map<String, String> headers) {
        // 构建请求头
        HttpHeaders httpHeaders = new HttpHeaders();
        if (headers != null) {
            headers.forEach(httpHeaders::add);
        }

        // 发送请求
        ResponseEntity<String> response = restTemplate.exchange(
                url,
                HttpMethod.DELETE,
                new HttpEntity<>(httpHeaders),
                String.class
        );

        // 返回响应内容
        return response.getBody();
    }
}
